<template>
  <div class="header">
    <div class="homeIcon">
      <el-icon
        size="30px"
        @click="handleCollapse"
        :style="{ transform: isCollapse ? '' : 'rotate(180deg)' }"
        ><expand
      /></el-icon>
      <span>首页</span>
    </div>
    <!-- mouseenter 鼠标移进该盒子 -->
    <div
      class="user"
      @mouseenter="isShowUserInfo('show')"
      @mouseleave="isShowUserInfo('leave')"
    >
      <!-- src 动态绑定值 -->
      <img :src="userInfo.headImg" alt="" />
      <div class="userInfo" v-show="show">
        <!-- 取值 -->
        <div>{{ userInfo.name }}</div>
        <div @click="loginOut">退出登录</div>
      </div>
    </div>
  </div>
</template>
<script setup>
import router from '../router/index';
import { transform } from 'lodash';
import { ref, onMounted, defineProps, reactive } from 'vue';
import { getUserInfo } from '../api/index';
/**
 * 获取父组件的参数
 */
const { handleCollapse, isCollapse } = defineProps([
  'handleCollapse',
  'isCollapse',
]);

/**
 * 鼠标移动个人信息的展示
 */
const show = ref(false);
const isShowUserInfo = (type) => {
  type === 'show' ? (show.value = true) : (show.value = false);
};

/**
 * 获取用户信息
 */
const userInfo = reactive({
  name: '',
  headImg: '',
});
/**
 * 用户信息接口的调用 须在页面初始化的时候执行
 */
const getUserInfoData = async () => {
  const res = await getUserInfo();
  userInfo.name = res.name;
  userInfo.headImg = require('../assets/headImg/'+res.headImg);
};

onMounted(() => {
  getUserInfoData();
});

/**
 * 退出登录按钮
 */
const loginOut = () => {
  router.push('/login');
  localStorage.removeItem('token');
};
</script>
<style lang="less" scoped>
.userInfo {
  z-index: 22;
  display: flex;
  flex-direction: column;
  position: absolute;
  right: 0;
  bottom: -70px;
  background-color: #fff;
  border: 5px;
  box-shadow: 0 4px 8px 0 rgb(7 17 27 / 10%);
  text-align: center;

  div:hover {
    color: #409eff;
  }

  div {
    padding: 10px;
  }
}
// https://blog.csdn.net/qq_44537625/article/details/100040169 讲解关于弹性布局
.header {
  position: relative; //相对定位 保留原来位置
  height: 100%;
  align-items: center; //定义项目在竖直方向上对齐方式
  display: flex;
  justify-content: space-between; /* 均匀排列每个元素 首个元素放置于起点，末尾元素放置于终点 */

  .homeIcon {
    display: flex;
    align-items: center;

    span {
      margin-left: 10px;
    }
  }

  .user {
    display: flex;
    justify-content: center;
    width: 80px;

    img {
      width: 45px;
      height: 45px;
      border-radius: 50%;
    }
  }
}
</style>
